#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use lib "/net/cpp-group/Leo/bin";
my $old_db_name= 'rat_genome';
my $new_db_name	= 'rat_genome';
my $remote_host	= 'fgu251';
my $from_user	= 'postgres';
my $to_user		= 'postgres';
#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888
my $usage = <<"USAGE";

USAGE:

    copy_db_to_bulk.pl
                            [--old_db_name "$old_db_name"]
                            [--new_db_name "$new_db_name"]
                            [--remote_host "$remote_host"]
                            [--from_user "$from_user"]
                            [--to_user "$to_user"]
                            [--help]

        This script replicates a specified database, overwriting if necessary
USAGE

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory


# optional parameters
my $help = undef;
GetOptions(
			'new_db_name=s'		=> \$new_db_name,
			'old_db_name=s'		=> \$old_db_name,
			'remote_host=s'		=> \$remote_host,
			'help'				=> \$help);

die $usage if ($help);


$remote_host = "-h $remote_host" if (length $remote_host);
$to_user = "-U $to_user" if (length $to_user);
$from_user = "-U $from_user" if (length $from_user);
print("dropdb $remote_host $to_user $new_db_name\n");
system("dropdb $remote_host $to_user $new_db_name");
print("createdb $remote_host $to_user $new_db_name\n");
system("createdb $remote_host $to_user $new_db_name");
print("pg_dump $from_user $old_db_name | psql $remote_host $to_user $new_db_name\n");
system("pg_dump $from_user $old_db_name | psql $remote_host $to_user $new_db_name");
print ("\nFinished\n\n");

